﻿using cuiail.Controllers;
using System;

namespace cuiail.DBModels
{
    public class datastruct
    {
    }

    /// <summary>
    /// 系统运行模式
    /// </summary>
    /// 
    //
    public enum SystemMode
    {
        /// <summary>
        /// Demo mode
        /// </summary>
        SingleDemoMode = 1,
        /// <summary>
        /// 循环演示模式
        /// </summary>
        LoopDemoMode = 2,

        /// <summary>
        /// Interactive mode
        /// </summary>
        InteractiveMode = 3,

        /// <summary>
        /// Fault mode
        /// </summary>
        FaultMode = 4,
    }
    /// <summary>
    /// 设备运行状态模式
    /// </summary>
    /// 
    public enum DeviceRunStatus
    {
        /// <summary>
        /// Paused status
        /// </summary>
        Paused = 0,

        /// <summary>
        /// Running status
        /// </summary>
        Running = 1,

        /// <summary>
        /// Fault status
        /// </summary>
        Fault = 2,

        /// <summary>
        /// Reset status
        /// </summary>
        Reset = 3,
    }

    //设备编号，装配岛1，搬运机器人2，质检岛3，巡检机器人4
    public enum DeviceID
    {
        /// <summary>
        /// 装配岛
        /// </summary>
        AssemblyIsland = 1,
        /// <summary>
        /// 搬运机器人
        /// </summary>
        MobileRobot = 2,
        /// <summary>
        /// 质检岛
        /// </summary>
        QualityInspectionIsland = 3,
        /// <summary>
        /// 巡检机器人
        /// </summary>
        InspectionRobot = 4,
    }


    //命令状态反馈，状态结果+返回信息
    public class CommandStatusFeedback
    {
        /// <summary>
        /// 状态结果
        /// </summary>
        public bool StatusResult { get; set; }
        /// <summary>
        /// 返回信息
        /// </summary>
        public string? ReturnMessage { get; set; }
        public CommandStatusFeedback()
        {
            StatusResult = false;
            ReturnMessage = string.Empty;
        }
        public CommandStatusFeedback(bool statusResult, string returnMessage)
        {
            StatusResult = statusResult;
            ReturnMessage = returnMessage;
        }
    }

    ///
    /// 设备运行命令
    /// 
    public enum DeviceRunCommand
    {
        /// <summary>
        /// Stop command
        /// </summary>
        Stop = 0,

        /// <summary>
        /// Start command
        /// </summary>
        Start = 1,

        /// <summary>
        /// Reset command
        /// </summary>
        Reset = 2,
    }
    //质检通过	结构体	上一个质检产品编号+质检结果
    public class QualityInspectionResult
    {
        /// <summary>
        /// 上一个质检产品编号
        /// </summary>
        public string? PreviousInspectionProductId { get; set; }

        /// <summary>
        /// 质检结果
        /// </summary>
        public bool InspectionResult { get; set; }
    }
    ///<summary>
    ///机械臂末端点位姿
    /// </summary>
    /// 
    public class RobotEndPose
    {
        public double J1 { get; set; }
        public double J2 { get; set; }
        public double J3 { get; set; }
        public double J4 { get; set; }
        public double J5 { get; set; }
        public double J6 { get; set; }
    }

    ///<summary>
    ///
    ///移动机器人坐标
    ///
    public class MobileRobotPose
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Theta { get; set; }
    }

    /// <summary>
    /// 错误信息坐标
    /// 
    public class ErrorInfo
    {
        public int ErrorCode { get; set; }
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 当前工序生产信息
    /// 当前装配产品（string）+完成状态，完成状态int，1完成，2进行中
    ///
    public class CurrentProcessInfo
    {
        /// <summary>
        /// 产品编号
        /// </summary>
        public string? ProductId { get; set; }
        /// <summary>
        /// 工序序号ID，用于组织工序流程，自动追踪到前到工序和后道工序
        /// </summary>
        public int ProcessId { get; set; }
        /// <summary>
        /// 工序序号ID，用于组织工序流程，自动追踪到前到工序和后道工序
        /// </summary>

        public ProcessCompletionStatus CompletionStatus { get; set; } = 0;
    }
    //枚举工序状态0初始状态 1未开始，2前道工序可释放，3进行中，4完成
    public enum ProcessCompletionStatus
    {
        /// <summary>
        /// 初始状态
        /// </summary>
        Initial = 0,
        /// <summary>
        /// 未开始
        /// </summary>
        NotStarted = 1,
        /// <summary>
        /// 前道工序可释放
        /// </summary>
        PreviousProcessReleasable = 2,
        /// <summary>
        /// 进行中
        /// </summary>
        InProgress = 3,
        /// <summary>
        /// 完成
        /// </summary>
        Completed = 4
    }

    public class PositionOffset
    {
        public double X_Offset { get; set; }
        public double Y_Offset { get; set; }
    }

    // 搬运机器人允许取料信号	结构体	取料产品编号（string）+（可以取料）bool。

    public class RobotPickupSignal
    {
        /// <summary>
        /// 取料产品编号
        /// </summary>
        public string? PickupProductId { get; set; }

        /// <summary>
        /// 可以取料
        /// </summary>
        public bool CanPickup { get; set; }
    }
    //
    public class DownloadProductInfo
    {
        /// <summary>
        /// 产品编号
        /// </summary>
        public string ProductId { get; set; } = "";


        /// <summary>
        /// 产品打印数据
        /// </summary>
        public string ProductPrintData { get; set; } = "";
        /// <summary>
        /// 附件信息
        /// </summary>
        public string addmes { get; set; }
    }


    //工序状态信息
    //包括，工序编号+工序名称+工序状态（int，1完成，2进行中，3未开始（物料交接中，物料正在拿起，））
    public class ProcessStatusInfo
    {
        /// <summary>
        /// 工序编号
        /// </summary>
        public int ProcessId { get; set; }
        /// <summary>
        /// 工序名称
        /// </summary>
        public string? ProcessName { get; set; }
        /// <summary>
        /// 工序状态（int，0未开始，1交接中，2进行中，3完成（物料交接中，物料正在拿起，））
        /// </summary>
        public ProcessCompletionStatus ProcessStatus { get; set; }
    }

    //单个产品追踪信息类
    /// 产品编号+当前产品工序状态（）+所在的工序，这里只是物品所在工序。
    /// 工序有交接过程，这里中写明产品实际在工序步。
    public class UploadProductTraceInfo
    {
        /// <summary>
        /// 产品编号
        /// </summary>
        public string? ProductId { get; set; }
        /// <summary>
        /// 当前产品工序状态集，这样可以追踪每个工序的状态
        /// </summary>
        public ProcessStatusInfo ProcessStatus { get; set; }
        /// <summary>
        /// 是否加工结束
        /// </summary>
        public bool IsProcessingComplete { get; set; }
    }
    //定义工序名称
    //工序id，+工序名称
    public class ProcessNameInfo
    {
        /// <summary>
        /// 工序id
        /// </summary>
        public int ProcessId { get; set; }
        /// <summary>
        /// 工序名称
        /// </summary>
        public string? ProcessName { get; set; }
        /// <summary>
        /// 工序属性，是否为可重复工序，是否可并行工序 
        /// 0,工序可重复，正常。
        /// 1工序为单循环，只有整个产品生成完成才可下发
        /// 2工序为最终工序，只有工单结束才会触发
        /// </summary>
        /// 
        public int ProcessAttribute { get; set; }

        /// <summary>
        /// 是否为结束属性,0不是，1单循环结束，2最终循环结束
        /// </summary>
        /// 
        public int IsEndAttribute
        {
            get; set;
        }
        //工序绑定的设备ID
        public DeviceID DeviceId { get; set; }
        //正在处理的工序编号
        public string CurrentProcessProductId { get; set; } = "";
        public ProcessCompletionStatus CurrentProcessCompletionStatus { get; set; } = 0;
    }
    //定义不同模式下的工序列表
    public class ModeProcessList
    {
        /// <summary>
        /// 模式id
        /// </summary>
        public int ModeId { get; set; }
        /// <summary>
        /// 模式名称
        /// </summary>
        public string? ModeName { get; set; }
        /// <summary>
        /// 工序列表
        /// </summary>
        public List<ProcessNameInfo>? ProcessList { get; set; }

        public void clearCurrentProcessInfo()
        {
            if (ProcessList==null || ProcessList.Count ==0)
            {
                return;
            }
            foreach (var process in ProcessList)
            {
                process.CurrentProcessProductId = "";
                process.CurrentProcessCompletionStatus = ProcessCompletionStatus.Initial;
                _工单管理类.FindDevicProductInfo(process.DeviceId).ProductId="";
            }
        }

        //定义多个工序，1装配工序，2搬运工序，3质检工序，4拆解工序
        public static ProcessNameInfo _装配工序dy = new ProcessNameInfo { ProcessId = 1, ProcessName = "装配工序", ProcessAttribute =1, IsEndAttribute=0, DeviceId = DeviceID.AssemblyIsland };
        public static ProcessNameInfo _搬运工序dy = new ProcessNameInfo { ProcessId = 2, ProcessName = "搬运工序", ProcessAttribute =1, IsEndAttribute=0, DeviceId =DeviceID.MobileRobot };
        public static ProcessNameInfo _质检工序dy = new ProcessNameInfo { ProcessId = 3, ProcessName = "质检工序", ProcessAttribute =1, IsEndAttribute=0, DeviceId =DeviceID.QualityInspectionIsland };
        public static ProcessNameInfo _拆解工序dy = new ProcessNameInfo { ProcessId = 4, ProcessName = "拆解工序", ProcessAttribute =1, IsEndAttribute=2, DeviceId =DeviceID.MobileRobot };

        public static ProcessNameInfo _装配工序xy = new ProcessNameInfo { ProcessId = 1, ProcessName = "装配工序", ProcessAttribute =0, IsEndAttribute=0, DeviceId =DeviceID.AssemblyIsland };
        public static ProcessNameInfo _搬运工序xy = new ProcessNameInfo { ProcessId = 2, ProcessName = "搬运工序", ProcessAttribute =0, IsEndAttribute=0, DeviceId =DeviceID.MobileRobot };
        public static ProcessNameInfo _质检工序xy = new ProcessNameInfo { ProcessId = 3, ProcessName = "质检工序", ProcessAttribute =0, IsEndAttribute=2, DeviceId =DeviceID.QualityInspectionIsland };
        // public static ProcessNameInfo _拆解工序xy = new ProcessNameInfo { ProcessId = 4, ProcessName = "拆解工序", ProcessAttribute = 2, IsEndAttribute=2, DeviceId =DeviceID.MobileRobot };

        public static ProcessNameInfo _镭雕工序ys = new ProcessNameInfo { ProcessId = 1, ProcessName = "镭雕工序", ProcessAttribute =0, IsEndAttribute=0, DeviceId =DeviceID.QualityInspectionIsland };
        public static ProcessNameInfo _搬运工序ys = new ProcessNameInfo { ProcessId = 2, ProcessName = "搬运工序", ProcessAttribute =0, IsEndAttribute=2, DeviceId =DeviceID.MobileRobot };
        //定义静态模式，1，单次演示模式，2 循环演示模式，3互动模式
        public static ModeProcessList SingleDemoMode = new ModeProcessList
        {
            ModeId = 1,
            ModeName = "单次演示模式",
            ProcessList = new List<ProcessNameInfo> { _装配工序dy, _搬运工序dy, _质检工序dy, _拆解工序dy }
        };
        public static ModeProcessList LoopDemoMode = new ModeProcessList
        {
            ModeId = 2,
            ModeName = "循环演示模式",
            ProcessList = new List<ProcessNameInfo> { _装配工序xy, _搬运工序xy, _质检工序xy}
        };
        public static ModeProcessList InteractiveMode = new ModeProcessList
        {
            ModeId = 3,
            ModeName = "互动模式",
            ProcessList = new List<ProcessNameInfo> { _镭雕工序ys, _搬运工序ys }
        };
    }
    //设备ID与工序ID绑定类
    public class DeviceProcessBinding
    {
        public DeviceID DeviceId { get; set; }
        public int ProcessId { get; set; }

        public static List<DeviceProcessBinding> DeviceProcessBindings = new List<DeviceProcessBinding>
        {
            new DeviceProcessBinding { DeviceId = DeviceID.AssemblyIsland, ProcessId = 0 },
            new DeviceProcessBinding { DeviceId = DeviceID.MobileRobot, ProcessId = 0 },
            new DeviceProcessBinding { DeviceId = DeviceID.QualityInspectionIsland, ProcessId = 0 },
        };
    }

    //工序切换方法
    //根据当前云平台下发的工单信息，模式类别，当前产品状态信息，返回当前产品需要执行的下一个工序。
    //工单开始操作包括，将下发的工单列表
    public class _工单管理类
    {

        public static void WorkOrderStart()
        {

            ModeProcessList modeProcessList = new ModeProcessList();
            if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.InteractiveMode)
            {
                modeProcessList = ModeProcessList.InteractiveMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.LoopDemoMode)
            {
                modeProcessList = ModeProcessList.LoopDemoMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.SingleDemoMode)
            {
                modeProcessList = ModeProcessList.SingleDemoMode;
            }
            //初始化工单信息
            if (CloudPlatformController.cloudPlatformPosWorkOrder==null)
            {
                return;
            }
            if (CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable == null ||CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable.Count ==0)
            {
                return;
            }
            //
            CloudPlatformController.cloudWorkOrder.WorkOrderNumber = CloudPlatformController.cloudPlatformPosWorkOrder.WorkOrderNumber;
            CloudPlatformController.cloudWorkOrder.ProductType = CloudPlatformController.cloudPlatformPosWorkOrder.ProductType;
            CloudPlatformController.cloudWorkOrder.ProductIdTable.Clear();
            foreach (var item in CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable)
            {
                CloudPlatformController.cloudWorkOrder.ProductIdTable.Add(new UploadProductTraceInfo
                {
                    ProductId = item.ProductId,
                    IsProcessingComplete = false,
                    ProcessStatus = new ProcessStatusInfo
                    {
                        ProcessId = 0,
                        ProcessName = "",
                        ProcessStatus = ProcessCompletionStatus.Initial
                    }
                });
            }
            CloudPlatformController.cloudWorkOrder.WorkOrderStartTime = DateTime.Now;
            CloudPlatformController.cloudWorkOrder.WorkOrderCompletionStatus = "进行中";
            //清除modeProcessList中各工序的当前处理产品信息
            modeProcessList.clearCurrentProcessInfo();
            //清除发给各工序的流程的ID信息



        }

        public static void WorkOrderStopAndEnd()
        {

            ModeProcessList modeProcessList = new ModeProcessList();
            if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.InteractiveMode)
            {
                modeProcessList = ModeProcessList.InteractiveMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.LoopDemoMode)
            {
                modeProcessList = ModeProcessList.LoopDemoMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.SingleDemoMode)
            {
                modeProcessList = ModeProcessList.SingleDemoMode;
            }
            //初始化工单信息
            if (CloudPlatformController.cloudPlatformPosWorkOrder==null)
            {
                return;
            }
            if (CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable == null ||CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable.Count ==0)
            {
                return;
            }


            CloudPlatformController.cloudWorkOrder.WorkOrderStartTime = DateTime.Now;
            CloudPlatformController.cloudWorkOrder.WorkOrderCompletionStatus = "已完成";
            //清除modeProcessList中各工序的当前处理产品信息
            modeProcessList.clearCurrentProcessInfo();


        }

        public static void _编号工序流转(CloudPlatGetWorkOrderData cloudPlatGetWorkOrderData, DownloadProductInfo _工序产品获取, CurrentProcessInfo upcurrentProcessInfo)
        {
            //工序ID赋值给对应类别

            try
            {
                ModeProcessList modeProcessList = new ModeProcessList();
                if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.InteractiveMode)
                {
                    modeProcessList = ModeProcessList.InteractiveMode;
                }
                else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.LoopDemoMode)
                {
                    modeProcessList = ModeProcessList.LoopDemoMode;
                }
                else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.SingleDemoMode)
                {
                    modeProcessList = ModeProcessList.SingleDemoMode;
                }
                //在modeProcessList中找到当前工序
                //如果增加一个下位向上位的请求生成物料编号，那我就不需要考虑上到工序完成到下到工序拿走之间的时间差问题了
                //下单工序请求生产新物料，则意味着，下单工序当前已经完成。这样，我只需要根据前到工序的完成状态，
                //如完成，则直接赋值给下到工序当前物料编号即可。
                //如果各工序不进行主动请求生产编号，那么生产编号的跳转就需要考虑时间差问题，工序完成并不能作为编号跳转的依据，
                //必须是下道工序清空了，才能进行编号的跳转。得有一个清空逻辑。清空逻辑
                //首先判断工单信息里面有没有需要处理的产品编号
                if (cloudPlatGetWorkOrderData == null)
                {
                    return;
                }
                if (cloudPlatGetWorkOrderData.ProductIdTable == null || cloudPlatGetWorkOrderData.ProductIdTable.Count ==0)
                {
                    return;
                }
                if (cloudPlatGetWorkOrderData.WorkOrderCompletionStatus!="进行中")
                {
                    return;
                }
                //首先更新  modeProcessList，更新当前工序的产品编号和状态，找到modeProcessList中对应的工序，如果编号一致，则更新状态。
                if (!string.IsNullOrWhiteSpace(upcurrentProcessInfo.ProductId))
                {
                    foreach (var process in modeProcessList.ProcessList)
                    {
                        if (process.ProcessId == upcurrentProcessInfo.ProcessId)
                        {
                            if (process.CurrentProcessProductId== upcurrentProcessInfo.ProductId)
                            {
                                process.CurrentProcessCompletionStatus = upcurrentProcessInfo.CompletionStatus;
                            }
                            break;
                        }
                    }
                }
                //更新
                UpdateWorkOrderProcessStatus(CloudPlatformController.cloudWorkOrder);
                //判断当前工序是否为初始状态
                if (upcurrentProcessInfo.ProcessId==1)
                {
                    if (upcurrentProcessInfo.ProductId==null || upcurrentProcessInfo.ProductId=="")
                    {
                        //寻找工单中状态为未开始的产品编号，赋值给当前工序
                        int index2 = cloudPlatGetWorkOrderData.FindFirstNotStartedProductIndex();
                        if (index2 != -1)
                        {
                            modeProcessList.ProcessList[0].CurrentProcessProductId = cloudPlatGetWorkOrderData.ProductIdTable[index2].ProductId;
                            modeProcessList.ProcessList[0].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                            _工序产品获取.ProductId = modeProcessList.ProcessList[0].CurrentProcessProductId;
                            _工序产品获取.ProductPrintData =CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable[index2].ProductPrintData;
                        }
                    }
                }
                //如果是最后的工序，则不进行流转
                int index3 = modeProcessList.ProcessList.FindIndex(x => x.ProcessId == upcurrentProcessInfo.ProcessId);
                if (index3!=-1)
                {
                    //如果是正常循环的最后工序步
                    if (modeProcessList.ProcessList[index3].IsEndAttribute ==1)
                    {
                        //判断是否要那
                        if (upcurrentProcessInfo.ProductId == null|| upcurrentProcessInfo.ProductId=="")
                        {
                            //判断前一各工序状态是否是完成状态，如果是则将前一工序的编号赋值给新工序
                            if (index3-1>=0)
                            {
                                if (modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                                {
                                    modeProcessList.ProcessList[index3].CurrentProcessProductId = modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId;
                                    modeProcessList.ProcessList[index3].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                    _工序产品获取.ProductId = modeProcessList.ProcessList[index3].CurrentProcessProductId;
                                    //寻找工单中对应的产品打印数据

                                }

                            }

                        }
                        //如果当前工序为完成状态，并且不是最后可以直接进行清空
                        if (upcurrentProcessInfo.CompletionStatus == ProcessCompletionStatus.Completed)
                        {
                            //如果当前ID为最后一个产品ID
                            if (cloudPlatGetWorkOrderData.IsLastProduct(upcurrentProcessInfo.ProductId))
                            {
                                modeProcessList.ProcessList[index3+1].CurrentProcessProductId=upcurrentProcessInfo.ProductId;
                                modeProcessList.ProcessList[index3+1].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                //更新生产ID

                            }
                            else
                            {
                                cloudPlatGetWorkOrderData.ProductIdTable.Find(x => x.ProductId == upcurrentProcessInfo.ProductId).IsProcessingComplete = true;
                                modeProcessList.ProcessList[index3].CurrentProcessProductId = "";
                                modeProcessList.ProcessList[index3].CurrentProcessCompletionStatus =ProcessCompletionStatus.NotStarted;

                                _工序产品获取.ProductId = modeProcessList.ProcessList[index3].CurrentProcessProductId;
                            }

                        }
                        //如果完成状态是前道工序可释放
                        if (upcurrentProcessInfo.CompletionStatus == ProcessCompletionStatus.PreviousProcessReleasable)
                        {
                            //判断前一各工序状态是否是完成状态，如果是则将前一工序的编号赋值给新工序
                            if (index3-1>=0)
                            {
                                if (modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                                {
                                    if (modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId ==upcurrentProcessInfo.ProductId)
                                    {
                                        //前工序完成，可以释放，清空编号
                                        modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId = "";
                                        modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                        // _前工序产品编码获取.ProductId =modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId;
                                        FindDevicProductInfo(modeProcessList.ProcessList[index3 - 1].DeviceId).ProductId = "";
                                    }
                                    //寻找工单中对应的产品打印数据
                                }
                            }
                            //
                            //判断本工序是否完成


                        }
                    }
                    //如果就是最后一步
                    if (modeProcessList.ProcessList[index3].IsEndAttribute ==2)
                    {
                        if (upcurrentProcessInfo.CompletionStatus == ProcessCompletionStatus.Completed)
                        {
                            modeProcessList.ProcessList[index3].CurrentProcessProductId = "";
                            modeProcessList.ProcessList[index3].CurrentProcessCompletionStatus =ProcessCompletionStatus.NotStarted;
                            _工序产品获取.ProductId = modeProcessList.ProcessList[index3].CurrentProcessProductId;
                            if (cloudPlatGetWorkOrderData.ProductIdTable.Find(x => x.ProductId == upcurrentProcessInfo.ProductId)!=null)
                            {
                                cloudPlatGetWorkOrderData.ProductIdTable.Find(x => x.ProductId == upcurrentProcessInfo.ProductId).IsProcessingComplete = true;
                            }

                            //判断是否为最后的产品
                            if (cloudPlatGetWorkOrderData.IsLastProduct(upcurrentProcessInfo.ProductId))
                            {
                                cloudPlatGetWorkOrderData.WorkOrderCompletionStatus = "已完成";
                                cloudPlatGetWorkOrderData.WorkOrderEndTime = DateTime.Now;
                            }

                            //cloudPlatGetWorkOrderData.WorkOrderCompletionStatus = "已完成";
                            //cloudPlatGetWorkOrderData.WorkOrderEndTime = DateTime.Now;
                        }
                        if (upcurrentProcessInfo.CompletionStatus == ProcessCompletionStatus.PreviousProcessReleasable)
                        {
                            if (index3-1>=0)
                            {
                                if (modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                                {
                                    if (modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId ==upcurrentProcessInfo.ProductId)
                                    {
                                        //前工序完成，可以释放，清空编号
                                        modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId = "";
                                        modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                        // _前工序产品编码获取.ProductId =modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId;
                                        FindDevicProductInfo(modeProcessList.ProcessList[index3 - 1].DeviceId).ProductId = "";
                                    }
                                    //寻找工单中对应的产品打印数据
                                }
                            }
                        }
                        //判断是否要那
                        if (upcurrentProcessInfo.ProductId == null|| upcurrentProcessInfo.ProductId=="")
                        {
                            //判断前一各工序状态是否是完成状态，如果是则将前一工序的编号赋值给新工序
                            if (index3-1>=0)
                            {
                                if (modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                                {
                                    modeProcessList.ProcessList[index3].CurrentProcessProductId = modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId;
                                    modeProcessList.ProcessList[index3].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                    _工序产品获取.ProductId = modeProcessList.ProcessList[index3].CurrentProcessProductId;
                                    //寻找工单中对应的产品打印数据

                                }

                            }

                        }
                    }
                    //如果不是最后工序
                    if (modeProcessList.ProcessList[index3].IsEndAttribute ==0)
                    {

                        if (upcurrentProcessInfo.ProductId == null|| upcurrentProcessInfo.ProductId=="")
                        {
                            //判断前一各工序状态是否是完成状态，如果是则将前一工序的编号赋值给新工序
                            if (index3-1>=0)
                            {
                                if (modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                                {
                                    modeProcessList.ProcessList[index3].CurrentProcessProductId = modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId;
                                    modeProcessList.ProcessList[index3].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                    _工序产品获取.ProductId = modeProcessList.ProcessList[index3].CurrentProcessProductId;
                                    //寻找工单中对应的产品打印数据
                                }

                            }

                        }
                        //如果单前工序提交前道工序可释放状态，则判断签到工序是否为完成状态，如果是完成状态，则清空前道工序的产品编号
                        if (upcurrentProcessInfo.CompletionStatus == ProcessCompletionStatus.PreviousProcessReleasable)
                        {
                            if (index3-1>=0)
                            {
                                if (modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                                {
                                    if (modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId ==upcurrentProcessInfo.ProductId)
                                    {
                                        //前工序完成，可以释放，清空编号
                                        modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId = "";
                                        modeProcessList.ProcessList[index3 - 1].CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                                        // _前工序产品编码获取.ProductId =modeProcessList.ProcessList[index3 - 1].CurrentProcessProductId;
                                        FindDevicProductInfo(modeProcessList.ProcessList[index3 - 1].DeviceId).ProductId = "";
                                    }
                                    //寻找工单中对应的产品打印数据
                                }
                            }
                        }

                    }


                }
            }
            catch (Exception ex)
            {

            }
        }

        //根据modelist信息，更新当前工单中的产品工序状态
        public static void UpdateWorkOrderProcessStatus(CloudPlatGetWorkOrderData cloudPlatGetWorkOrderData)
        {

            ModeProcessList modeProcessList = null;
            if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.InteractiveMode)
            {
                modeProcessList = ModeProcessList.InteractiveMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.LoopDemoMode)
            {
                modeProcessList = ModeProcessList.LoopDemoMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.SingleDemoMode)
            {
                modeProcessList = ModeProcessList.SingleDemoMode;
            }
            if (modeProcessList==null)
            {
                return;
            }

            if (cloudPlatGetWorkOrderData == null)
            {
                return;
            }
            if (cloudPlatGetWorkOrderData.ProductIdTable == null || cloudPlatGetWorkOrderData.ProductIdTable.Count == 0)
            {
                return;
            }
            foreach (var product in cloudPlatGetWorkOrderData.ProductIdTable)
            {
                foreach (var process in modeProcessList.ProcessList)
                {
                    if (process.CurrentProcessCompletionStatus== ProcessCompletionStatus.InProgress || process.CurrentProcessCompletionStatus == ProcessCompletionStatus.Completed)
                    {
                        if (product.ProductId== process.CurrentProcessProductId)
                        {
                            product.ProcessStatus.ProcessId = process.ProcessId;

                            product.ProcessStatus.ProcessName = process.ProcessName;

                            product.ProcessStatus.ProcessStatus = process.CurrentProcessCompletionStatus;
                            break;
                        }
                    }

                }
            }
        }

        //更具设备ID找到对应的编号类
        public static DownloadProductInfo FindDevicProductInfo(DeviceID deviceID)
        {
            switch (deviceID)
            {
                case DeviceID.AssemblyIsland:
                    return AssemblyIslandController.assemblyIslandCommand._当前生成产品信息;
                case DeviceID.MobileRobot:
                    return TransportRobotController.transportRobotCommand._当前生成产品信息;
                case DeviceID.QualityInspectionIsland:
                    return QualityIslandController.qualityIslandCommand._当前生成产品信息;
                default:
                    return new DownloadProductInfo();
            }
        }

        //根据工序序号反向确认modelist，然后清除对应modelist的状态记录
        public static void ClearModeProcessListByWorkOrderId(int processID)
        {
            if (CloudPlatformController.cloudPlatformPosWorkOrder == null)
            {
                return;
            }

            ModeProcessList modeProcessList = new ModeProcessList();
            if (CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.InteractiveMode)
            {
                modeProcessList = ModeProcessList.InteractiveMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.LoopDemoMode)
            {
                modeProcessList = ModeProcessList.LoopDemoMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.SingleDemoMode)
            {
                modeProcessList = ModeProcessList.SingleDemoMode;
            }

            if (modeProcessList != null && modeProcessList.ProcessList != null)
            {
                var process = modeProcessList.ProcessList.Find(x => x.ProcessId == processID);
                if (process != null)
                {
                    process.CurrentProcessProductId = "";
                    process.CurrentProcessCompletionStatus = ProcessCompletionStatus.NotStarted;
                }
            }
        }

        //产品工序ID分配
        public static int AssignProcessIdByDeviceId(DeviceID deviceID)
        {
            //根据所选流程表来定义ID
            ModeProcessList modeProcessList =null;
            if (CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.InteractiveMode)
            {
                modeProcessList = ModeProcessList.InteractiveMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.LoopDemoMode)
            {
                modeProcessList = ModeProcessList.LoopDemoMode;
            }
            else if (CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.SingleDemoMode)
            {
                modeProcessList = ModeProcessList.SingleDemoMode;
            }

            if(modeProcessList==null)
            {
                return -1;
            }
            if(CloudPlatformController.cloudSystemModel.WorkingModeId == SystemMode.SingleDemoMode)
            {

                if(deviceID== DeviceID.MobileRobot)
                {
                    if (modeProcessList.ProcessList[2].CurrentProcessProductId != "")
                    {
                        //如果质检工序有产品编号，则说明是拆解工序在运行
                        return modeProcessList.ProcessList[3].ProcessId;
                    }
                    else
                    {
                        //否则是搬运工序在运行
                        return modeProcessList.ProcessList[1].ProcessId;
                    }
                }
                else
                {
                    return modeProcessList.ProcessList.Find(x => x.DeviceId == deviceID)?.ProcessId ?? -1;
                }
            }
            else
            {
                return modeProcessList.ProcessList.Find(x => x.DeviceId == deviceID)?.ProcessId ?? -1;
            }
        }
    }

    //工单开始，工单停止，工单继续，工单强制停止并结束，工单继续剩余工件从头开始，工单复位
    public enum WorkOrderCommand
    {
        /// <summary>
        /// Start work order
        /// </summary>
        Start = 1,
        /// <summary>
        /// Pause work order
        /// </summary>
        Pause = 2,
        /// <summary>
        /// Resume work order
        /// </summary>
        Resume = 3,
        /// <summary>
        /// Stop and end work order
        /// </summary>
        StopAndEnd = 4,
        /// <summary>
        /// Resume remaining work from start
        /// </summary>
        ResumeFromStart = 5,
        /// <summary>
        /// Reset work order
        /// </summary>
        Reset = 6,
    }

}
